﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using TrackerRealm.Jazz.Common;

namespace TrackerRealm.Jazz.Client
{
    /// <summary>
    /// Wraps a 'MethodInfo' object representing a proptery write accessor (setter).
    /// <para>Note: Property setter and getters are methods.</para>
    /// </summary>
    public class jWriteAccessor: jMethodInfo
    {
        /// <summary>
        /// 
        /// </summary>
        public static jWriteAccessor Empty = new jWriteAccessorEmpty();
        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="method"></param>
        /// <param name="prop"></param>
        /// <param name="jObj"></param>
        internal jWriteAccessor(MethodInfo method, jPropertyInfo prop, jObject jObj)
        :base(method, jObj)
        {
            this.Property = prop;
            if (prop != null)  // will be null when empty
            {
                if (prop.IsUI) this.isUI = true;
            }
            //if (prop != null)  // will be null when empty
            //{
            //    prop.CanWrite = true;
            //    if (prop.IsUI) this.isUI = true;
            //    if (jObj != null)
            //    {
            //        prop.CanWrite = method.IsGrantedAccess(jObj);
            //    }
            //}
        }
        #endregion
        #region Property
        /// <summary>
        /// Returns the Property that contains this 'Write' accessor.
        /// </summary>
        public jPropertyInfo Property
        {   get;
            private set;
        }
        #endregion
        #region CanWrite
        /// <summary>
        /// Returns true if 'IsProperty' is true and the 'Write' accessor is usable.
        /// </summary>
        public override bool CanWrite
        {
            get{ return this.Property.CanWrite; }
        }
        #endregion
        #region Set
        /// <summary>
        /// 
        /// </summary>
        /// <param name="wf"></param>
        /// <param name="setObj"></param>
        /// <returns></returns>
        public virtual object Set(jObject wf, object setObj)
        {
            return this.MethodInfo.Invoke(wf, new Object[] { setObj });
        }
        #endregion
        #region IsWriteAccessor
        /// <summary>
        /// Returns true if this member is a property setter.
        /// <para>Note: Property setter and getters are methods.</para>
        /// </summary>
        public override bool IsWriteAccessor
        {
            get { return true; }
        }
        #endregion
        #region Empty SetterInfo Class
        /// <summary>
        /// 
        /// </summary>
        private class jWriteAccessorEmpty : jWriteAccessor
        {
            #region Constructor
            /// <summary>
            /// Constructor
            /// </summary>
            internal jWriteAccessorEmpty()
                : base(null, null, null)
            {
                this.Name = "";
            }
            #endregion
            #region IsEmpty
            /// <summary>
            /// 
            /// </summary>
            public override bool IsEmpty
            {
                get
                {
                    return true;
                }
            }
            #endregion
            #region CanWrite
            /// <summary>
            /// Returns true if 'IsProperty' is true and the 'Write' accessor is usable.
            /// </summary>
            public override bool CanWrite
            {
                get
                {
                    return false;
                }
            }
            #endregion
            #region Set
            /// <summary>
            /// 
            /// </summary>
            /// <param name="wf"></param>
            /// <param name="setObj"></param>
            /// <returns></returns>
            public override object Set(jObject wf, object setObj)
            {
                throw new AccessViolationException();
            }
            #endregion
        }
        #endregion
    }
}
